package com.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

import com.SampleListView.UserInfo;

public class MyDatabase {
    
	public static final String DATABASE_NAME = "mydatabase";
    public static final String USER_TABLE_NAME = "user";
    public static final String USER_DESC_TABLE_NAME = "desc";
    private static final String TAG = "MyDatabase";

    private DatabaseHelper mOpenHelper;
    
    public MyDatabase(Context context) {
    	mOpenHelper = new DatabaseHelper(context);
    }
    
    /**
     * Returns Cursor타입
     * 
     * @return
     */
    public Cursor getUserList() {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(USER_TABLE_NAME);
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        
        return qb.query(db, null, null, null, null, null, "name ASC");
    }   
    
    
    public UserInfo getFolderInfoFull(String UserId) {
    	UserInfo userDesc = null;
    	
    	SQLiteDatabase db = null;
    	Cursor c = null;
    	try
        {
	    	db = mOpenHelper.getReadableDatabase();
	    	String q = "select user.*, desc.desc from user left join desc on user.id = desc.id where user.id = '" + UserId +"'";
	        c = db.rawQuery(q, null);
	        while (c.moveToNext()) {
	        	String desc = c.getString(c.getColumnIndex(ColumnsUserDesc.DESC));
	        	 Log.i(TAG, desc);
	        	if (userDesc == null) {
	        		 Log.i(TAG, "folder == null");
	        		 userDesc = new UserInfo(desc);
	        	}
	        	
	        }
        }
        finally {
        	if (c != null) {
        		c.close();
        	}
        }
        return userDesc;
    }    
    /**
     * 데이타 베이스 닫기
     */
    public void close() {
    	mOpenHelper.close();
    }
}
